﻿using System;
using System.Web.Mvc;
using Epi.Web.MVC.Facade;
using Epi.Web.MVC.Models;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.XPath;
using Epi.Core.EnterInterpreter;
using System.Web.Security;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Configuration;
using System.Web.Routing;
using System.Web.WebPages;
using System.Web.Caching;
using System.Reflection;
using System.Diagnostics;
 
using Epi.Web.Enter.Common.Message;
using Epi.Web.Enter.Common.DTO;
using Epi.Web.MVC.Utility;
using System.Linq;
using System.Web.Configuration;

namespace Epi.Web.MVC.Controllers
{
     [Authorize]
    public class LinkInfoController : Controller
    {


        private ISurveyFacade _isurveyFacade;
        public LinkInfoController(ISurveyFacade isurveyFacade)
        {
            _isurveyFacade = isurveyFacade;
        }
        // GET: /LinkInfo/
    [HttpGet]
        public ActionResult Index(string FromId ,string ToId,int ViewId,bool IsNewLink = false)
        {
        LinkInfoModel Model = new LinkInfoModel();
        Model.RecordFromId = FromId;
        Model.RecordToId = ToId;
        Model.ViewId = ViewId;
        Model.IsNewLink = IsNewLink;

        return View("Index", Model);
        }


    [HttpPost]
    public ActionResult Link(
        string FromResponseId,
        string ToResponseId,
        DateTime DateOfLastContact,
        bool DateIsEstimated
        , bool ContactTypeOpt1
        , bool ContactTypeOpt2
        , bool ContactTypeOpt3
        , bool ContactTypeOpt4
        , bool TentativeRelationship,
        string RelationToCase,
        bool IsNewLink = false
        )
    {

        FormLinkResponseRequest FormLinkResponseRequest = new FormLinkResponseRequest();
        SurveyAnswerRequest pRequest = new SurveyAnswerRequest();
        pRequest.Criteria = new Enter.Common.Criteria.SurveyAnswerCriteria();
        pRequest.Criteria.SurveyId = Session["RootFormId"].ToString();
        pRequest.Criteria.UserId = SurveyHelper.GetDecryptUserId(Session["UserId"].ToString());
        pRequest.Criteria.SurveyAnswerIdList.Add(ToResponseId);
        FormLinksDTO FormLinksDTO = new Enter.Common.DTO.FormLinksDTO();
        FormLinksDTO.FromId = FromResponseId;
        FormLinksDTO.ToId = ToResponseId;
        FormLinksDTO.DateOfLastContact = DateOfLastContact;
        FormLinksDTO.DateIsEstimated = DateIsEstimated;
        FormLinksDTO.ContactTypeOpt1 = ContactTypeOpt1;
        FormLinksDTO.ContactTypeOpt2 = ContactTypeOpt2;
        FormLinksDTO.ContactTypeOpt3 = ContactTypeOpt3;
        FormLinksDTO.ContactTypeOpt4 = ContactTypeOpt4;
        FormLinksDTO.TentativeRelation = TentativeRelationship;
        FormLinksDTO.RelationToSource = RelationToCase;
        FormLinkResponseRequest.FormResponseLinks.Add(FormLinksDTO);
        if (IsNewLink)
        {

            FormLinkResponseRequest.Action = "Update";
        }


        _isurveyFacade.Link(FormLinkResponseRequest,  pRequest);
         
        return Json(string.Empty);


    }

    }
}
